﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using Axis.Dao.Research;
using Axis.Web.Administrator.Global;
using Axis.BusinessObject.Research;
using System.Web.SessionState;
using Axis.BusinessObject.Core;

namespace Axis.Web.Administrator.Views.Project
{
    public class CQuestionUI : System.Web.UI.Page
    {
        public string GetXmlRequest(HttpContext context)
        {
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("Root");

            XmlElement InputValue = doc.CreateElement("InputValue");
            HttpRequest request = context.Request;
            root.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            root.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());
            if (request["ProjectID"] != null)
                InputValue.SetAttribute("ProjectID", request["ProjectID"].Trim());
            if (request["DependentID"] != null)
                InputValue.SetAttribute("DependentID", request["DependentID"].Trim());
            if (request["ID"] != null)
                InputValue.SetAttribute("ID", request["ID"].Trim());
            if (request["Type"] != null)
                InputValue.SetAttribute("Type", request["Type"].Trim());
            if (request["QuestionText"] != null)
                InputValue.SetAttribute("QuestionText", request["QuestionText"].Trim());
            if (request["Type"] != null)
                InputValue.SetAttribute("Type", request["Type"].Trim());
            if (request["Action"] != null)
                InputValue.SetAttribute("Action", request["Action"].Trim());
            if (request["ZOrder"] != null)
                InputValue.SetAttribute("ZOrder", request["ZOrder"].Trim());
            if (request["ScaleFromText"] != null)
                InputValue.SetAttribute("ScaleFromText", request["ScaleFromText"].Trim());
            if (request["ScaleToText"] != null)
                InputValue.SetAttribute("ScaleToText", request["ScaleToText"].Trim());
            if (request["ScaleFrom"] != null)
                InputValue.SetAttribute("ScaleFrom", request["ScaleFrom"].Trim());
            if (request["ScaleTo"] != null)
                InputValue.SetAttribute("ScaleTo", request["ScaleTo"].Trim());
            if (request["Code"] != null)
                InputValue.SetAttribute("Code", request["Code"].Trim());
            if (request["MaxResponseCount"] != null)
                InputValue.SetAttribute("MaxResponseCount", request["MaxResponseCount"].Trim());
            if (request["UserID"] != null)
                InputValue.SetAttribute("UserID", request["UserID"].Trim());
            if (request["SecrectToken"] != null)
                InputValue.SetAttribute("SecrectToken", request["SecrectToken"].Trim());
            if (request["DisplayRandomResponse"] != null)
                InputValue.SetAttribute("DisplayRandomResponse", request["DisplayRandomResponse"].Trim());
            if (request["DependentType"] != null)
                InputValue.SetAttribute("DependentType", request["DependentType"].Trim());
            if (request["ContentType"] != null)
                InputValue.SetAttribute("ContentType", request["ContentType"].Trim());
            else
                InputValue.SetAttribute("ContentType", "0");
            if (request["MediaUrl"] != null)
                InputValue.SetAttribute("MediaUrl", request["MediaUrl"].Trim());
            root.AppendChild(InputValue);

            if (request["ResponseOption"] != null)
            {
                CResponseUI responseUI = new CResponseUI();
                string temp = HttpUtility.HtmlDecode(request["ResponseOption"].Trim());
                IList<CResponseOption> List = responseUI.GetResponseList(temp);
                foreach (CResponseOption row in List)
                {
                    XmlElement ele = doc.CreateElement("ResponseOption");
                    ele.SetAttribute("ID", row.ID.ToString());
                    ele.SetAttribute("Caption", row.Caption.ToString());
                    ele.SetAttribute("Value", row.Value.ToString());
                    ele.SetAttribute("Type", row.Type.ToString());
                    ele.SetAttribute("Description", row.Description.ToString());
                    ele.SetAttribute("AllowInputText", row.AllowInputText.ToString());
                    ele.SetAttribute("ZOrder", row.ZOrder.ToString());
                    ele.SetAttribute("ScaleFrom", row.ScaleFrom.ToString());
                    ele.SetAttribute("ScaleFromText", row.ScaleFromText.ToString());
                    ele.SetAttribute("ScaleTo", row.ScaleTo.ToString());
                    ele.SetAttribute("ScaleToText", row.ScaleToText.ToString());
                    root.AppendChild(ele);
                }
                IList<CResponseOption> ListCol = responseUI.GetResponseListCol(temp);
                foreach (CResponseOption row in ListCol)
                {
                    XmlElement ele = doc.CreateElement("SubQuestionnaire");
                    ele.SetAttribute("ID", row.ID.ToString());
                    ele.SetAttribute("QuestionText", row.Caption.ToString());
                    ele.SetAttribute("Value", row.Value.ToString());
                    ele.SetAttribute("Description", row.Description.ToString());
                    ele.SetAttribute("ZOrder", row.ZOrder.ToString());
                    ele.SetAttribute("ScaleFrom", row.ScaleFrom.ToString());
                    ele.SetAttribute("ScaleFromText", row.ScaleFromText.ToString());
                    ele.SetAttribute("ScaleTo", row.ScaleTo.ToString());
                    ele.SetAttribute("ScaleToText", row.ScaleToText.ToString());
                    root.AppendChild(ele);
                }
            }
            return root.InnerXml;
        }
        public IList<CQuestionaire> GetQuestionareList(int QuestionnaireID, int ProjectID)
        {
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            CResearchDao core = new CResearchDao();
            return core.GetQuestionaire(string.Format("<InputValue UserID='{0}' ID='{1}' ProjectID='{2}'/>", member.ID.ToString(), QuestionnaireID, ProjectID));
        }
        public IList<CQuestionaire> GetQuestionareList(int ProjectID)
        {
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            CResearchDao core = new CResearchDao();
            return core.GetQuestionaire(string.Format("<InputValue UserID='{0}' ProjectID='{1}'  RowsPerPage='200' PageIndex='1' />", member.ID.ToString(), ProjectID));
        }
        public IList<CQuestionaire> GetQuestionareListByAdmin(int QuestionnaireID, int ProjectID)
        {
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            CResearchDao core = new CResearchDao();
            return core.GetQuestionaire(string.Format("<InputValue UserID='{0}' ID='{1}' ProjectID='{2}' context ='ADMIN'/>", member.ID.ToString(), QuestionnaireID, ProjectID));
        }
    }
}